package com.studyyoun.android.androidcommonbaselibrary.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by androidlongs on 2018/3/10.
 * 检查 权限工具类
 */

public class PermissionsCheckerUtils {
    private final Context mContext;

    public PermissionsCheckerUtils(Context lContext) {
        mContext = lContext;
    }
    private boolean laksPermissions(String... permissions){
        for (String permission : permissions) {
            if (lacksPermission(permission)){
                return true;
            }
        }
        return false;
    }
    //判断是否缺少仅限
    private boolean lacksPermission(String lPermission) {
        return ContextCompat.checkSelfPermission(mContext,lPermission)== PackageManager.PERMISSION_DENIED;
    }

    //定位相关权限
    private final String  coarseLocationPermission = Manifest.permission.ACCESS_COARSE_LOCATION;
    private final String  fineLocationPermission = Manifest.permission.ACCESS_FINE_LOCATION;
    private final String  readPhonePermission = Manifest.permission.READ_PHONE_STATE;
    public static final int REQUESET_LOCATION_CODE_STATE=101;

    //判断是否有定位权限
    public boolean isLocationPermission(){
        if (Build.VERSION.SDK_INT>=23){
           return laksPermissions(coarseLocationPermission,fineLocationPermission,readPhonePermission);
        }
        return true ;
    }
    //申请定位权限
    public void requestLocationPermission(Activity lActivity){
        ActivityCompat.requestPermissions(lActivity,new String[]{coarseLocationPermission,fineLocationPermission,readPhonePermission},REQUESET_LOCATION_CODE_STATE);
    }



}

